#include "cuda_mdle.h"
#include "ui_cuda_mdle.h"

#include <iostream>
#include <random>
#include <chrono>
#include <QTimer>
#include <qelapsedtimer.h>
#include "cuda_dll.h"
#include <QDebug>
cuda_mdle::cuda_mdle(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::cuda_mdle)
{
    ui->setupUi(this);
}

cuda_mdle::~cuda_mdle()
{
    delete ui;
}

void cuda_mdle::on_ui_cuda_test_clicked()
{
    QElapsedTimer timer;
    timer.start();
    // --------------------------------------------
    // 运算测试
    const int arraySize = 5;
    for(int j = 0; j < 10; j++)
    {
        int d[arraySize];
        generateRandomNumbers(d,arraySize);
        int b[arraySize];
        generateRandomNumbers(b,arraySize);
        int e[arraySize] = {0};
        if(vectorAdd(e, d, b, arraySize)!=0)
        {
            qDebug() << "cuda run error";
        }
        for(int i = 0; i < arraySize; i++)
        {
            ui->ui_cuda_show->append(QString::number(d[i])+"+"+QString::number(b[i])+"="+QString::number(e[i]));
        }
        ui->ui_cuda_show->append("GPU run once");
    }
    ui->ui_test_speed->setValue(timer.nsecsElapsed());
    // --------------------------------------------
    // 字符测试
    cuda_hello();
}

void cuda_mdle::generateRandomNumbers(int *array, int N)
{
    unsigned seed =
    std::chrono::system_clock::now().time_since_epoch().count();
    std::default_random_engine generator(seed);
    std::uniform_real_distribution<float> distribution(0.0, 1000); //range is 0 to 1
    for (int i = 0; i < N; ++i) {
        array[i] = (int)distribution(generator);
    }

}

